package com.product.magina.webserviceclient.service.impl;

import com.alibaba.fastjson.JSON;
import com.product.magina.webserviceclient.service.WsClientService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Service;

@Service(value = "wsClientService")
public class WsClientServiceImpl implements WsClientService {

    @Override
    public Object getMsg() {
    // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8081/services/TestService?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects = new Object[0];
        try {
            DemoModel model = new DemoModel();
            model.setId("id000001");
            model.setUserName("I am id000001!");
            String json = JSON.toJSONString(model);
            // invoke("方法名",参数1,参数2,参数3....);
            Object[] objArray = new Object[2];
            objArray[1] = json;
            objArray[0] = "001";
            objects = client.invoke("sendMessageById", objArray);
            // invoke("方法名",参数1,参数2,参数3....);
//            objects = client.invoke("sendMessage", "Magina");
            System.out.println("返回数据:" + objects[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objects[0];
    }

    @Override
    public Object saveMsg() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8081/services/TestService?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects = new Object[0];
        try {
            DemoModel model = new DemoModel();
            model.setId("id000001");
            model.setUserName("I am id000001!");
            String json = JSON.toJSONString(model);
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("saveMessage", json);

            // invoke("方法名",参数1,参数2,参数3....);
//            objects = client.invoke("sendMessage", "Magina");
            System.out.println("返回数据:" + objects[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objects[0];
    }
}
